{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/34_extract_values.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# Interactive extraction of pixel values and interactive region reduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Interactive extraction of pixel values"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "### Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "### Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "### Add data to the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "landsat7 = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\").select([0, 1, 2, 3, 4, 6])\n",
    "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n",
    "Map.addLayer(landsat7, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\")\n",
    "\n",
    "Map.set_plot_options(add_marker_cluster=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "### Activate the plotting tool\n",
    "\n",
    "Tick the `Plotting` checkbox and click the mouse on the map to start displaying charts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "### Export pixel values to shapefile/csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "# out_csv = os.path.join(out_dir, 'points.csv')\n",
    "out_shp = os.path.join(out_dir, \"points.shp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.extract_values_to_points(out_shp)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "## Interactive Region Reduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "### Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "### Create an interactive map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "### Add add to the map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = (\n",
    "    ee.ImageCollection(\"MODIS/006/MOD13A2\")\n",
    "    .filterDate(\"2015-01-01\", \"2019-12-31\")\n",
    "    .select(\"NDVI\")\n",
    ")\n",
    "\n",
    "# Convert the image collection to an image.\n",
    "image = collection.toBands()\n",
    "\n",
    "ndvi_vis = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 9000.0,\n",
    "    \"palette\": [\n",
    "        \"FFFFFF\",\n",
    "        \"CE7E45\",\n",
    "        \"DF923D\",\n",
    "        \"F1B555\",\n",
    "        \"FCD163\",\n",
    "        \"99B718\",\n",
    "        \"74A901\",\n",
    "        \"66A000\",\n",
    "        \"529400\",\n",
    "        \"3E8601\",\n",
    "        \"207401\",\n",
    "        \"056201\",\n",
    "        \"004C00\",\n",
    "        \"023B01\",\n",
    "        \"012E01\",\n",
    "        \"011D01\",\n",
    "        \"011301\",\n",
    "    ],\n",
    "}\n",
    "\n",
    "m.addLayer(image, {}, \"MODIS NDVI Time-series\")\n",
    "m.addLayer(image.select(0), ndvi_vis, \"MODIS NDVI VIS\")\n",
    "\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21",
   "metadata": {},
   "source": [
    "### Set reducer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.set_plot_options(add_marker_cluster=True, marker=None)\n",
    "m.roi_reducer = ee.Reducer.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23",
   "metadata": {},
   "source": [
    "### Export data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n",
    "# out_csv = os.path.join(out_dir, 'points.csv')\n",
    "out_shp = os.path.join(out_dir, \"ndvi.shp\")\n",
    "m.extract_values_to_points(out_shp)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
